<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>芝士erp角色管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/resources/css/public.css" media="all"/>
    <link rel="stylesheet" href="/resources/layui_ext/dtree/dtree.css" media="all"/>
    <link rel="stylesheet" href="/resources/layui_ext/dtree/font/dtreefont.css" media="all"/>
</head>
<body class="childrenBody">
<!--query condition start-->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 5px;">
    <legend>查询条件</legend>
</fieldset>
<blockquote class="layui-elem-quote">
    <form action="" method="post" id="searchForm" lay-filter="searchForm" class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">用户名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">备注信息</label>
                <div class="layui-input-inline">
                    <input type="text" name="remark" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">开始时间</label>
                <div class="layui-input-inline">
                    <input type="tel" name="startDate" id="startDate" readonly="readonly" placeholder="年-月-日 时:分:秒"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">结束时间</label>
                <div class="layui-input-inline">
                    <input type="tel" name="endDate" id="endDate" readonly="readonly" placeholder="年-月-日 时:分:秒"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">是否可用</label>
                <div class="layui-input-inline">
                    <input type="radio" name="available" value="1" title="可用">
                    <input type="radio" name="available" value="0" title="不可用">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: right;">
                <button type="button" class="layui-btn layui-btn-sm" shiro:hasPermission="permission:view" lay-submit="" lay-filter="doSearch"><span
                        class="layui-icon layui-icon-search"></span>查询
                </button>
                <button type="reset" class="layui-btn layui-btn-warm layui-btn-sm"><span
                        class="layui-icon layui-icon-refresh-1"></span>重置
                </button>
            </div>
        </div>
    </form>
</blockquote>
<!--query condition end-->

<!--form start-->
<div>
    <div id="roleToolBar" style="display: none;">
        <button type="button" lay-event="addRole" shiro:hasPermission="role:create" class="layui-btn layui-btn-sm">
            <span class="layui-icon layui-icon-add-1"></span>添加角色
        </button>
        <button type="button" lay-event="batchDeleteRole" shiro:hasPermission="role:batchDelete" class="layui-btn layui-btn-sm layui-btn-danger"><span
                class="layui-icon layui-icon-delete"></span>批量删除
        </button>
    </div>

    <table class="layui-hide" id="roleTable" lay-filter="roleTable"></table>

    <div id="roleRowBar" style="display: none;">
        <button type="button" lay-event="updateRole" shiro:hasPermission="role:update" class="layui-btn layui-btn-sm"><span
                class="layui-icon layui-icon-edit"></span>更新
        </button>
        <button type="button" lay-event="deleteRole" shiro:hasPermission="role:delete" class="layui-btn layui-btn-sm layui-btn-danger"><span
                class="layui-icon layui-icon-delete"></span>删除
        </button>
        <button type="button" lay-event="assignPermission" shiro:hasPermission="role:selectPermission" class="layui-btn layui-btn-sm">
            <span class="layui-icon layui-icon-about"></span>分配权限
        </button>

    </div>
</div>
<!--form end-->

<!--富文本界面的隐藏和弹出-->
<div style="display: none;padding: 5px" id="addOrUpdateDiv">
    <form action="" method="post" class="layui-form layui-form-pane" id="dataForm" lay-filter="dataForm">
        <div class="layui-form-item">
            <label class="layui-form-label layui-bg-gray">角色名称</label>
            <div class="layui-input-block">
                <input type="hidden" name="id">
                <input type="text" name="name" lay-verify="required" autocomplete="off" placeholder="请输入角色名称"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-bg-gray">信息备注</label>
            <div class="layui-input-block">
                <textarea name="remark" id="content" class="layui-textarea" autocomplete="off"
                          placeholder="请输入备注信息"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label layui-bg-gray">是否可用</label>
                <div class="layui-input-inline">
                    <input type="radio" name="available" value="1" title="可用" checked="">
                    <input type="radio" name="available" value="0" title="不可用">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: right;">
                <button type="button" class="layui-btn layui-btn-sm" lay-submit="" id="doSubmit"><span
                        class="layui-icon layui-icon-add-1"></span>提交
                </button>
                <button type="reset" class="layui-btn layui-btn-warm layui-btn-sm"><span
                        class="layui-icon layui-icon-refresh-1"></span>重置
                </button>

            </div>
        </div>
    </form>

</div>

<!--查看角色的弹出层-->
<div style="display: none;padding: 5px" id="assignPermissionDiv">
    <ul id="permissionTree" class="dtree" data-id="0"></ul>
</div>

<script type="text/javascript" src="/resources/layui/layui.js"></script>
<script type="text/javascript" src="/resources/layui_ext/dtree/dtree.js"></script>
<script type="text/javascript" th:inline="none">
    layui.extend({
        dtree: '/resources/layui_ext/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
    }).use(['jquery', 'table', 'form', 'layer', 'laydate', 'layedit', 'dtree'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var layedit = layui.layedit;
        var dtree = layui.dtree;

        //初始化一下富文本，给我们添加角色提供一个书写的地方
        var editIndex = layedit.build('content');

        //初始化时间选择器
        laydate.render({
            elem: '#startDate',
            type: 'datetime'
        });
        laydate.render({
            elem: '#endDate',
            type: 'datetime'
        });

        //加载数据
        var tableIns = table.render({
            elem: '#roleTable',
            url: '/role/loadAllRole',
            toolbar: '#roleToolBar', //开启头部工具栏
            title: '角色数据表',
            height: 'full-222',
            page: true,
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field: 'id', title: 'ID', align: 'center', width: '80'},
                {field: 'name', title: '角色名称', align: 'center', width: '150'},
                {field: 'remark', title: '信息备注', align: 'center', width: '320'},
                {
                    field: 'available', title: '是否可用', align: 'center', width: '100', templet: function (d) {
                        return d.available == 1 ? '<font color=blue>可用</font>' : '<font color=red>不可用</font>';
                    }
                },
                {field: 'createtime', title: '创建时间', align: 'center', width: '200'},
                {fixed: 'right', title: '操作', toolbar: '#roleRowBar', align: 'center', width: '375'}
            ]],
            //done:数据渲染完的回调，你可以借此做一些其他的操作
            done: function (res, curr, count) {//处理删除最后一页的bug
                if (res.data.length == 0 && curr != 1) {
                    tableIns.reload({
                        page: {
                            curr: (curr - 1)
                        }
                    });
                }
            }
        });

        //模糊查询
        form.on("submit(doSearch)", function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });

        //监听数据表格顶部的工具条
        table.on("toolbar(roleTable)", function (obj) {
            switch (obj.event) {
                case 'batchDeleteRole':
                    batchDeleteRole();
                    break;
                case 'addRole':
                    addRole();
                    break;
            }
        });
        //监听数据表格当中的行工具条
        table.on("tool(roleTable)", function (obj) {
            switch (obj.event) {
                case 'deleteRole':
                    deleteRole(obj.data);
                    break;
                case 'updateRole':
                    updateRole(obj.data);
                    break;
                case 'assignPermission':
                    assignPermission(obj.data);
                    break;
            }
        });

        var mainIndex;
        var url;

        function addRole() {
            mainIndex = layer.open({
                type: 1,
                content: $("#addOrUpdateDiv"),
                area: ['800px', '500px'],
                title: '发布角色',
                success: function () {
                    $("#dataForm")[0].reset();
                    layedit.setContent(editIndex, "");
                    url = "/role/addRole";
                }
            })
        }

        function updateRole(data) {
            mainIndex = layer.open({
                type: 1,
                content: $("#addOrUpdateDiv"),
                area: ['800px', '500px'],
                title: '修改角色',
                success: function () {
                    $('#dataForm')[0].reset();
                    //装载新的数据
                    form.val("dataForm", data);
                    layedit.setContent(editIndex, data.remark);
                    url = "/role/updateRole";
                }
            })
        }

        //这里因为富文本和textarea是不同步的，所以如果我们加了lay-verify验证的话，那么直接提交，框架是会给我们爆出content为空的验证错误。
        //所以为了能够让富文本和textarea之间同步，我们可以做一定的同步操作。
        $("#doSubmit").click(function () {
            layedit.sync(editIndex);
            var data = $("#dataForm").serialize();
            if (data.split("&")[1].split("=")[1] === "" || data.split("&")[2].split("=")[1] === "") {
                //此处无需打印，lay-verify会自动弹出提示信息，当然也可以打印。
                //例如layer.msg("标题或内容为空，无法提交！")
            } else {
                $.post(url, data, function (res) {
                    if (res.code === 200) {
                        tableIns.reload();
                    }
                    layer.msg(res.msg);
                    layer.close(mainIndex);
                })
            }
        })

        function assignPermission(data) {
            mainIndex = layer.open({
                type: 1,
                content: $("#assignPermissionDiv"),
                area: ['800px', '500px'],
                title: '分配【' + data.name + '】权限',
                btn: ['<span class="layui-icon">&#xe605;确认分配</span>', '<span class="layui-icon">&#x1006;关闭窗口</span>'],
                yes: function (index, layero) {
                    //得到树选中的所有节点
                    var permissionData = dtree.getCheckbarNodesParam("permissionTree");
                    var params = "id=" + data.id;
                    $.each(permissionData, function (index, item) {
                        params += "&ids=" + item.nodeId;
                    });
                    //这里我虽然用字符串的形式一起传递角色的id值和权限的id值，但是后端用对象接收的话，它会根据相对应属性名称将其数值存放到对应的属性当中去
                    $.post("/role/saveRolePermission", params, function (res) {
                        layer.msg(res.msg);
                    })
                },
                btn2: function (index, layero) {

                },
                btnAlign: 'c',
                success: function () {
                    //根据角色ID请求权限和菜单tree的json数据
                    dtree.render({
                        elem: "#permissionTree",
                        url: "/role/initPermissionByRoleId?roleId=" + data.id,
                        dataStyle: "layuiStyle",
                        dataFormat: "list",
                        response: {message: "msg", statusCode: 0},
                        checkbar: true,
                        checkbarType: "all"
                    })
                }
            })
        }

        function batchDeleteRole() {
            var checkStatus = table.checkStatus('roleTable');
            var dataLength = checkStatus.data.length;
            if (dataLength > 0) {
                layer.confirm('你确定要删除这些角色？', {icon: 3, title: '提示'}, function (index) {
                    var data = checkStatus.data;
                    var ids = "";
                    $.each(data, function (index, item) {
                        if (index === 0) {
                            ids += 'ids=' + item.id;
                        } else {
                            ids += '&ids=' + item.id;
                        }
                    });
                    $.post('/role/batchDeleteRole', ids, function (res) {
                        if (res.code === 200) {
                            tableIns.reload();
                        }
                        layer.msg(res.msg);
                    })
                    layer.close(index);
                })
            } else {
                layer.msg("请选中操作行！")
            }

        }

        function deleteRole(data) {
            layer.confirm("你确定要删除【"+data.name+"】这个角色吗？", {icon: 3, title: '标题'}, function (index) {
                $.post('/role/deleteRoleById', {id: data.id}, function (res) {
                    if (res.code === 200) {
                        tableIns.reload();
                    }
                    layer.msg(res.msg);
                })
                layer.close(index);
            })
        }
    })
</script>

</body>
</html>